{% extends 'master.html' %}

{% block master %}
{% csrf_token %}
<div class="x-nav">
	<span class="layui-breadcrumb">
        <a href="">资产管理</a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
      </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" id="id_assetname" name="taskname" placeholder="资产名称" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" id="id_assetkey" name="assetkey" placeholder="资产标识" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select class="container" id="id_asset_type">
                                <option value="">资产类型</option>
                                {% for item in asset_type %}
                                    <option value="{{ item.id }}">{{ item.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </form>
                    <div class="layui-input-inline layui-show-xs-block UserTable">
                        <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="sreach">
                            <i class="layui-icon">&#xe615;</i></button>
                    </div>
                </div>
                <xblock>
                    <div class="layui-card-header UserTable">
                        <div class="layui-col-md10">
                            <button class="layui-btn " data-type="deletechoice"><i class="layui-icon">&#xe640;</i>删除资产</button>
                            <button class="layui-btn" onclick="x_admin_show('资产申请', '{% url 'assetrequest' %}')"><i class="layui-icon layui-icon-download-circle"></i>资产申请</button>
                            <button class="layui-btn" onclick="x_admin_show('添加资产', '{% url 'assetcreate' %}')"><i class="layui-icon layui-icon-add-1"></i>新增资产</button>
                            <button class="layui-btn" onclick="x_admin_show('批量导入', '{% url 'createuploadcsv' %}')"><i class="layui-icon layui-icon-add-1"></i>批量导入</button>

                            <button class="layui-btn " data-type="portchoice"><i class="layui-icon">&#xe666;</i>端口扫描</button>
                            <button class="layui-btn " data-type="scanchoice"><i class="layui-icon">&#xe672;</i>安全扫描</button>
                            <!--
{#                            {% if user.is_superuser %}#}
                                <button class="layui-btn " data-type="descoverchoice"><i class="layui-icon">&#xe628;</i>资产发现</button>
                                <button class="layui-btn " data-type="assetuserchoice"><i class="layui-icon">&#xe601;</i>资产指定</button>
{#                            {% endif %}#}
                            -->
                        </div>
                    <!--
                        <div class='layui-col-md2' align='right'>
                            <div><a href="">os_csv模板</a>&<a href="">web_csv模板</a></div>
                            <div><a href="">vuln_csv模板</a></div>
                        </div>
                    -->
                    </div>
                </xblock>
                <!-- task table-->
                <div class="layui-card-body">
                    <table class="layui-table layui-form" id="asset_table" lay-filter="asset"></table>
                </div>
                <!--end task table-->
            </div>
        </div>
    </div>
</div>


<script>
var csrf = $('input[name="csrfmiddlewaretoken"]').val();

layui.use('table', function () {
    var table = layui.table;
    //方法级渲染
    table.render({
        elem: '#asset_table'
        ,url: "{% url 'assetlist' %}"
        ,method: 'post'
        ,where: {'csrfmiddlewaretoken':csrf}
        ,cols: [[
            {checkbox: true, fixed: true}
            ,{field: 'asset_id', title: '资产编号', sort: true, fixed:true}
            ,{field: 'asset_name', title: '资产名称'}
            ,{field: 'asset_key', title: '资产标识', sort: true, event:'Asset'}
            ,{field: 'asset_type', title: '资产分类', sort: true}
            ,{field: 'asset_status', title: '资产状态',sort: true}
            ,{field: 'asset_inuse', title: '认领状态'}
            ,{field: 'user_email', title: '管理人员'}
            ,{field: 'asset_updatetime', title: '更新时间',sort: true}
            ,{title: '高级选项', fixed: 'right', align:'center', toolbar: '#barDemo'}
        ]]
        ,id: 'testReload'
        ,page: true
        ,limit: 15
        ,text:{none: '目前暂无数据'}
    });
    // 高级选项
    table.on('tool(asset)', function (obj) {
        var data = obj.data.asset_id;
        if (obj.event === 'Asset'){
            {#//url = "{% url 'details' '0' %}";#}
            //url = url.replace(/0/,data);
            //x_admin_show('资产详情',url);
            alert(data)
        }else if(obj.event === 'edit'){
            url = "{% url 'assetupdate' 'key' %}";
            url = url.replace(/key/,data);
            x_admin_show('资产详情',url,600);
        }else if(obj.event === 'detail'){
            url = "{% url 'assetdetails' '0' %}";
            url = url.replace(/0/,data);
            window.open(url);
        }
    });

    // ***功能操作***
    var $ = layui.$, active = {
        // 删除
        deletechoice: function () { //获取选中数据
            var checkStatus = table.checkStatus('testReload')
                ,data = checkStatus.data
                ,list = new Array();
            for(var i=0;i<data.length;i++){
                list[i] = data[i].asset_id
            }
            layer.confirm('该操作为敏感操作<br>点击确认，将该选中任务移除', function (index) {
                $.post("{% url 'assetdelete' %}", {'asset_id_list':JSON.stringify(list), 'action':'delete', 'csrfmiddlewaretoken':csrf}, function (res) {
                    layer.msg(res.error);
                    setTimeout(function () {
                        location.replace(location.href)
                    }, 2000)
                })
            })
        }
        //端口扫描
        ,portchoice: function () { //获取选中数据
            var checkStatus = table.checkStatus('testReload')
                ,data = checkStatus.data
                ,list = new Array()
            for(var i=0;i<data.length;i++){
                list[i] = data[i].asset_id
            }
            // alert(JSON.stringify(list));
            layer.confirm('该操作为敏感操作<br>点击确认，将会对所选资产进行端口扫描', function (index) {
                $.post("{% url 'assettaskaction' %}", {'asset_id_list':JSON.stringify(list), 'action':'port', 'csrfmiddlewaretoken':csrf}, function (res) {
                    layer.msg(res.error);
                    setTimeout(function () {
                        location.replace(location.href)
                    }, 2000)
                })
            })
        }
        //安全扫描
        ,scanchoice: function () { //获取选中数据
            var checkStatus = table.checkStatus('testReload')
                ,data = checkStatus.data
                ,list = ';';
            for(var i=0;i<data.length;i++){
                list[i] = data[i].asset_id + ';'
            }
            url = "{% url 'taskscanchoice' 'key' %}";
            url = url.replace(/key/,list);
            x_admin_show('新建扫描',url,600);
        }
        // 执行重载
        ,reload: function () {
            table.reload('testReload',{
                page: {
                    curr:1      //重第一页开始
                }
                ,where: {
                    name : $('#id_assetname').val()
                    ,key : $('#id_assetkey').val()
                    ,type : $('#id_asset_type option:selected').val()
                }
            });
        }
    };
    $('.UserTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});
</script>

<script type="text/html" id="barDemo">
    <div class="layui-btn-group">
        <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
        <a class="layui-btn layui-btn-xs" lay-event="detail"><i class="layui-icon">&#xe60a;</i>详情</a>
    </div>
</script>
{% endblock %}